<template>
  <KView class="shop-goods-view">
    <KView>
      <goods-item v-for="goods in goodsList" :key="`goods_${goods.id}`" :goods="goods" :buy-record="buyRecord" @buy-goods="buyGoods"
        @img-click="imgClick">
      </goods-item>
    </KView>
    <KView class="shop-goods-view-more" v-if="loading||hasNextData">
      <sys-loading v-if="loading" :size="60"></sys-loading>
      <KView v-else-if="hasNextData" class="shop-goods-view-btn" @click="lookMore">查看更多</KView>
    </KView>
    <KView class="shop-goods-view-null"></KView>

    <goods-model-dialog :visible.sync="openDialog" :goods="selectGoods" :buy-record="buyRecord" @buy-success="buySuccess" @close="dialogClose">
    </goods-model-dialog>
  </KView>
</template>

<script>
  import SysLoading from 'common/sysLoading';
  import GoodsModelDialog from '../goodsModelDialog';
  import GoodsItem from './goodsItem';

  export default {
    props: {
      goodsList: {
        type: Array,
        required: true
      },
      pagination: {
        type: Object,
        required: true
      },
      nextDataFun: {
        type: Function,
        required: true
      },
      buyRecord: {
        type: Object,
        required: true
      }
    },

    computed: {
      hasNextData() {
        return this.pagination.hasNextPage();
      }
    },

    data() {
      return {
        loading: false,
        openDialog: false,
        selectGoods: null
      };
    },

    methods: {
      buyGoods(goods) {
        this.selectGoods = goods;
        this.openDialog = true;
      },

      lookMore() {
        if (this.loading) {
          return;
        }

        let pagination = this.pagination;
        if (!pagination.hasNextPage()) {
          return;
        }

        pagination.nextPage();
        this.loading = true;
        this.nextDataFun().then(() => {
          this.loading = false;
        }).catch(() => {
          this.loading = false;
        });
      },

      imgClick(goods) {
        this.$emit('item-click', goods);
      },

      buySuccess() {
        this.$emit('buy-success');
      },

      dialogClose() {
        this.selectGoods = null;
      }
    },

    components: {
      SysLoading,
      GoodsModelDialog,
      GoodsItem
    }
  };

</script>

<style lang="scss">
  .shop-goods-view {
    height: 100%;
    overflow: auto;
    padding: 0 formatPx(30);

    .shop-goods-view-item {
      margin-bottom: formatPx(30);

      &:last-child {
        margin-bottom: 0;
      }
    }

    .shop-goods-view-more {
      position: relative;
      padding: formatPx(50) 0;
      height: formatPx(66);

      .shop-goods-view-btn {
        text-align: center;
        width: formatPx(255);
        height: 100%;
        line-height: formatPx(65);
        border: 1px solid #999999;
        font-size: formatPx(30);
        margin: auto;
        border-radius: formatPx(33);
      }
    }

    .shop-goods-view-null {
      height: formatPx(120);
    }
  }

</style>
